package com.ruoyi.system.service.carte.job.impl;

import com.ruoyi.system.domain.XxkCarte;
import com.ruoyi.system.domain.request.BaseTransRequest;
import com.ruoyi.system.domain.request.JobStartRequest;
import com.ruoyi.system.domain.request.TransStartRequest;
import com.ruoyi.system.service.carte.AbstractCarteService;
import com.ruoyi.system.service.carte.CarteConst;
import com.ruoyi.system.service.carte.job.IJobStart;
import com.ruoyi.system.service.carte.trans.ITransStart;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class JobStartImpl extends AbstractCarteService implements IJobStart {
    @Override
    public String startJob(JobStartRequest transRequest) {
        try{
            return commonHandle(transRequest);
        }catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return "error";
    }

    public  void setRequestUrl(XxkCarte xxkCarte, BaseTransRequest baseTransRequest ) throws Exception {
        JobStartRequest transRequest =(JobStartRequest)baseTransRequest;
        if(StringUtils.isBlank(transRequest.getXml())){
            transRequest.setXml("Y");
        }
        String url = "http://" + xxkCarte.getCarteIp() + ":" + xxkCarte.getCartePort()+ CarteConst.JOB_START_SUFFIXPATH;
        url = url.replace( " ", "%20" );
        transRequest.setRequestUrl(url);

        Map<String,Object> param=new HashMap<>();
        param.put("name",URLEncoder.encode( transRequest.getName(), "UTF-8" ));
        param.put("xml",transRequest.getXml());
        transRequest.setParmMap(param);

    }
}
